//
//  YBMessageVC.swift
//  Moonlight
//
//  Created by 清风徐来 on 2023/6/30.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import EachNavigationBar
import JXPagingView
import JXSegmentedView

class YBMessageVC: BaseVC {
    
    enum ItemType: Int {
        case system = 1
        case activity
        case news
        var des: String {
            switch self {
            case .system: return "公告"
            case .activity: return "活动"
            case .news: return "资讯"
            }
        }
    }
    private let items: [ItemType] = [.system, .activity, .news]

    override func viewDidLoad() {
        super.viewDidLoad()
        navigation.item.title = "消息"
        
        // Do any additional setup after loading the view.
        view.addSubview(pagingView)
        pagingView.addSubview(segmentedView)
        pagingView.snp.makeConstraints { (make) in
            make.top.equalTo(kNavigationBarHeight)
            make.left.bottom.right.equalToSuperview()
        }
        
        segmentedView.listContainer = pagingView.listContainerView
        
        guard let gesture = self.navigationController?.interactivePopGestureRecognizer else { return }
        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: gesture)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: gesture)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.bk_setPopSwipe(segmentedView.selectedIndex == 0)
    }
    
    // MARK: - lazy
    lazy var pagingView: JXPagingListRefreshView = {
        let pager = JXPagingListRefreshView(delegate: self)
        pager.backgroundColor = .lightGray242Dark17
        pager.mainTableView.gestureDelegate = self
        return pager
    }()
    
    lazy var segmentedView: JXSegmentedView = {
        let view = JXSegmentedView(frame: kCGRect(16, 0, kScreenWidth, 44))
        view.backgroundColor = .lightWhiteDark27
        view.delegate = self
        view.dataSource = segmentedDataSource
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = kRGBColor(46, 143, 255)
        indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
        indicator.indicatorHeight = 2
        indicator.lineStyle = .lengthenOffset
        view.indicators = [indicator]
        return view
    }()
    
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.titles = items.map { $0.des }
        dataSource.titleNormalFont = .systemFont(ofSize: 15, weight: .medium)
        dataSource.titleSelectedFont = .systemFont(ofSize: 15, weight: .medium)
        dataSource.titleNormalColor = .lightBlack51DarkLight230
        dataSource.titleSelectedColor = kRGBColor(46, 143, 255)
        dataSource.isTitleColorGradientEnabled = true
        dataSource.isSelectedAnimable = true
        return dataSource
    }()
    
}

// MARK: - JXPagingViewDelegate代理
extension YBMessageVC: JXPagingViewDelegate {
    
    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return 0
    }
    
    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return UIView()
    }
    
    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return 44
    }
    
    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }
    
    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return items.count
    }
    
    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        let item = items[index]
        let vc = YBMessageListVC(type: item)
        self.addChild(vc)
        return vc
        
    }
    
}

// MARK: - JXPagingMainTableViewGestureDelegate代理
extension YBMessageVC: JXPagingMainTableViewGestureDelegate {
    
    func mainTableViewGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if otherGestureRecognizer == segmentedView.collectionView.panGestureRecognizer { return false }
        return gestureRecognizer is UIPanGestureRecognizer && otherGestureRecognizer is UIPanGestureRecognizer
    }
    
}

// MARK: - JXSegmentedViewDelegate代理
extension YBMessageVC: JXSegmentedViewDelegate {
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.bk_setPopSwipe(index == 0)
    }
    
}
